package com.yixun.qfbao.hander;

import java.util.ArrayList;
import java.util.List;

import com.yixun.qfbao.api.service.DataPermissionService;
import com.yixun.qfbao.constant.RedisKeyConstant;
import com.yixun.qfbao.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.yixun.qfbao.apidto.BatchUpdateCaseMemberPremisesDto;
import com.yixun.qfbao.apidto.UpdateCaseMemberResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.Company;
import com.yixun.qfbao.model.dto.CompanyDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.service.CompanyService;
import com.yixun.qfbao.vo.BatchUpdateCaseMemberPremisesVo;
import com.yixun.qfbao.xf.model.dto.CaseMemberDto;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import com.yixun.qfbao.xf.service.CaseMemberService;

/**
 * 批量修改案场员工的设置
 * @author zilong.liu
 *
 */
@Service
public class BatchUpdateCaseMemberPremisesHandler extends CommonAbstractHander{
	
	private static final Logger LOGGER = LoggerFactory.getLogger(BatchUpdateCaseMemberPremisesHandler.class);

	@Autowired
	CaseMemberService caseMemberService;
	@Autowired
	CompanyService companyService;
	@Autowired
	DataPermissionService dataPermissionService;
	
	/**
	 * 批量修改案场员工订阅信息
	 */
	@Override
	public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
		
		BatchUpdateCaseMemberPremisesVo reqVo = (BatchUpdateCaseMemberPremisesVo)reqDto;
		BatchUpdateCaseMemberPremisesDto reqDto1 = reqVo.getParams();
		
		List<Long> userIds = reqDto1.getUserIds();
		List<Long> premisesIds = reqDto1.getPremisesIds();
		
		BaseResponseDTO<UpdateCaseMemberResDto> res = new BaseResponseDTO<>();
		UpdateCaseMemberResDto resDto = new UpdateCaseMemberResDto();
		if(CollectionUtils.isEmpty(userIds) || CollectionUtils.isEmpty(premisesIds)) {
			res.setResult(ResultEnum.SUCCESS.getNo());
	        return res;
		}
		
		String companyUuid = reqDto1.getCompanyUuid();
		CompanyDto companyDto = new CompanyDto();
		companyDto.setCompanyUuid(companyUuid);
		companyDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		Company company = companyService.selectByDto(companyDto);
		
		if(company == null) {
			resDto.setResult(ResultEnum.FAIL.getNo());
			resDto.setResultMsg("公司不存在");
			res.setResult(resDto.getResult());
			return res;
		}
		
		List<Long> userIds2 = new ArrayList<>();
		for (Long userId : userIds) {
			if(!userIds2.contains(userId)) {
				userIds2.add(userId);
			}
			// 清除用户缓存
			String redisKey = RedisKeyConstant.Permission.DEV_USER_PREMISES_PERMISSION + userId;
			RedisUtil.delete(redisKey);
		}
		
		CaseMemberDto caseMemberDto = new CaseMemberDto();
		caseMemberDto.setCompanyId(company.getId());
		caseMemberDto.setUserIds(userIds2);
		caseMemberDto.setPremisesIds(premisesIds);
        try{
            caseMemberService.batchUpdateCaseMemberPremises(caseMemberDto, premisesIds);
            resDto.setResult(ResultEnum.SUCCESS.getNo());
        }catch (Exception e){
            LOGGER.error("updateCaseMemberHander error: {}", e);
            resDto.setResult(ResultEnum.FAIL.getNo());
        }
        res.setResult(resDto.getResult());
        return res;
	}

}
